package com.naiterui.ehp.bs.cms.common.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalUnit;
import java.util.Date;

public class DateUtils {

    /**
     * 判断时间是不是今天
     *
     * @param date
     * @return 是返回true，不是返回false
     */
    public static boolean isNow(String date) {
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        //获取今天的日期
        String nowDay = sf.format(now);
        return date.equals(nowDay);
    }

    /**
     * 增加时间
     *
     * @param date
     * @param amountToAdd
     * @param unit        java.time.temporal.ChronoUnit
     * @return
     */
    public static Date plus(Date date, long amountToAdd, TemporalUnit unit) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.of("Asia/Shanghai");
        LocalDateTime newDate = LocalDateTime.ofInstant(instant, zone).plus(amountToAdd, unit);
        return Date.from(newDate.atZone(zone).toInstant());
    }

    /**
     * 将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd"，转换为Date
     *
     * @param dateStr
     * @return
     */
    public static Date parseStrToDate(String dateStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(dateStr, new ParsePosition(0));
    }
}
